<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb :closable="false">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card style="margin-top: 20px">
      <!-- 添加信息头部 -->
      <el-alert
        title="添加商品信息"
        type="info"
        center
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 步骤条 -->
      <el-steps
        :active="active - 0"
        finish-status="success"
        space="200px"
        style="margin-top: 20px; margin-left: 80px"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 侧边步骤条 -->
      <div class="rightContent">
        <el-form
          :rules="rules"
          ref="formRef"
          style="margin-top: 20px"
          label-position="top"
          label-width="80px"
          :model="form"
        >
          <el-tabs
            v-model="active"
            :tab-position="tabPosition"
            :before-leave="beforeTabLeave"
            @tab-click="onTabClick"
          >
            <!-- 1111111111111....基本信息 -->
            <el-tab-pane label="基本信息" name="0">
              <el-form-item label="商品名称" prop="goods_name">
                <el-input
                  style="width: 1450px"
                  v-model="form.goods_name"
                ></el-input>
              </el-form-item>
              <el-form-item label="商品价格" prop="goods_price">
                <el-input v-model="form.goods_price"></el-input>
              </el-form-item>
              <el-form-item label="商品数量" prop="goods_count">
                <el-input v-model="form.goods_number" type="number"></el-input>
              </el-form-item>
              <el-form-item label="商品重量" prop="goods_weight">
                <el-input v-model="form.goods_weight" type="number"></el-input>
              </el-form-item>
              <el-form-item label="商品分类" prop="goods_cat">
                <el-cascader
                  v-model="form.goods_cat"
                  :options="catList"
                  :props="catProps"
                  expand-trigger="hover"
                  @change="handleChange"
                  placeholder="请选择"
                >
                </el-cascader>
              </el-form-item>
            </el-tab-pane>
            <!-- 2222222222222....商品参数 -->
            <el-tab-pane label="商品参数" name="1">
              <!-- 2-1 -->
              <el-form-item
                :label="item.attr_name"
                v-for="item in parameter"
                :key="item.attr_id"
              >
                <el-checkbox-group v-model="item.attr_vals">
                  <el-checkbox
                    :label="it"
                    v-for="(it, i) in item.attr_vals"
                    :key="i"
                    border
                  ></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-tab-pane>
            <!-- 3333333333333....商品属性 -->
            <el-tab-pane label="商品属性" name="2">
              <el-form-item
                :label="item.attr_name"
                v-for="item in attribute"
                :key="item.attr_id"
              >
                <el-input :value="item.attr_vals"></el-input>
              </el-form-item>
            </el-tab-pane>
            <!-- 4444444444444....商品图片 -->
            <el-tab-pane label="商品图片" name="3">
              <el-upload
                :headers="headers"
                class="upload-demo"
                :action="upload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList"
                list-type="picture"
              >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-tab-pane>
            <!-- 5555555555555....商品内容 -->
            <el-tab-pane label="商品内容" name="4">
              <quill-editor
                v-model="form.content"
                style="width: 1500px; height: 500px; margin-bottom: 40px"
              ></quill-editor>
              <el-button
                type="primary"
                style="margin-top: 20px"
                @click="addForm"
                >添加商品</el-button
              >
            </el-tab-pane>
          </el-tabs>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  classification,
  getCategories,
  getAttribute,
  addAttribute
} from '@/api/good'
export default {
  name: 'addIndex',
  components: {
    quillEditor
  },
  data () {
    return {
      // 商品参数
      checked1: true,
      checked2: false,
      // 级联
      value: [],
      catList: [], // 级联数据列表

      // 步骤条,联动通过name点击哪个,哪个name就会给到active
      //   默认'0'给到第一个显示
      active: '0',
      tabPosition: 'left', // 侧边步骤条
      //   右侧商品添加
      labelPosition: 'right',
      form: {
        goods_name: '', // 商品名称
        goods_price: 0, // 商品价格
        goods_number: 0, // 商品数量
        goods_weight: 0, // 商品重量
        goods_cat: [], // 商品分类
        goods_introduce: '',
        content: '', // 编辑器内容
        attrs: []
      },
      //   校验
      rules: {
        goods_name: [
          {
            required: true,
            message: '请输入商品名称'
          }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格'
          }
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量'
          }
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量'
          }
        ],
        goods_cat: [
          {
            required: true,
            message: '请选择商品分类'
          }
        ]
      },
      // 级联
      catProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      // 添加商品参数
      parameter: [],
      // 商品属性
      attribute: [],
      // 上传图片
      fileList: [],
      // 图片上传地址
      upload: 'https://lianghj.top:8888/api/private/v1/upload',
      // 图片上传手动设置请求头
      headers: {
        Authorization: window.sessionStorage.getItem('token')
      }
    }
  },

  created () {
    this.obtainList()
  },

  methods: {
    // 上传图片
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    // 级联
    handleChange (value) {
      console.log(value)
      // console.log(this.form)
      if (this.form.goods_cat.length !== 3) {
        this.form.goods_cat = []
      }
    },
    // 侧边切换
    beforeTabLeave (newValue, oldValue) {
      console.log('即将离开' + oldValue)
      console.log('即将进入' + newValue)
      if (oldValue === '0' && this.form.goods_cat.length !== 3) {
        this.$message.error('请选择商品分类')
        return false
      }
    },
    // 可选框
    async obtainList () {
      const res = await classification()
      // console.log(res.data)
      this.catList = res.data
    },
    // 参数
    async onTabClick () {
      console.log(this.catList)
      if (this.active === '1') {
        const res = await getCategories(this.crateId)
        console.log(res.data)
        res.data.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length === 0 ? [] : item.attr_vals.split(' ')
        })
        // 参数渲染
        this.parameter = res.data
      } else if (this.active === '2') {
        console.log()
        const res = await getAttribute(this.crateId)
        console.log(res.data)
        this.attribute = res.data
      }
    },
    // 确认添加
    addForm () {
      this.$refs.formRef.validate(async (val) => {
        if (!val) {
          return this.$message.error('请填写必要的表单项')
        }
        this.form.goods_cat = this.form.goods_cat.join(',')
        const res = await addAttribute(this.form)
        console.log(res)
        this.$message.success(res.meta.msg)
        this.$router.push('/goods')
      })
    }
  },
  computed: {
    // // 获取参数
    crateId () {
      if (this.form.goods_cat.length === 3) {
        return this.form.goods_cat[2]
      }
      return null
    }
  }
}
</script>

<style scoped lang='scss'>
.rightContent {
  display: flex;
}
</style>
